package com.psa.system;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import com.psa.element.CustomerEditorElement;
import com.psa.element.Interchange;
import com.psa.tool.CreateSqlTools;
import com.psa.tool.DBTool;
import com.psa.tool.ExitApplication;
import com.psa.tool.SystemStatus;
import com.wxw.BaseLibrary.adapter.ElementListViewAdapter;
import com.wxw.BaseLibrary.tool.ActivityTool;
import net.duohuo.dhroid.ioc.InjectUtil;
import net.duohuo.dhroid.ioc.annotation.InjectView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *
 * @author david
 * @date 14-3-31
 */
public class CustomerActivity extends BaseActivity {
    @InjectView(id = R.id.btn_cold_drink, click = "onClick")
    private Button btn_cold_drink;
    @InjectView(id = R.id.lv_customer_info)
    private ListView lvRouteList;

    private List<CustomerEditorElement> customerEditorElements = new ArrayList<CustomerEditorElement>();
    private ElementListViewAdapter<CustomerEditorElement> adapter, canEditAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityTool.hideTitle(this);
        ActivityTool.fullScreen(this);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.customer_info_view);
        InjectUtil.inject(this);

        btn_next.setText(getString(R.string.customer_btn_next));
        btn_next.setBackgroundResource(R.drawable.button_style);
        tv_head_title.setText(SystemStatus.currentCustomer.getChineseName());

        lvRouteList = (ListView)findViewById(R.id.lv_customer_info);
        refreshCustomerInfo();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_back:
                SystemStatus.currentCustomer = null;
                goBack();
                break;
            case R.id.btn_next:
                Cursor cursor = DBTool.getInstance().query("Select * From Interchange Where Contents Like '%"+SystemStatus.currentCustomer.getID() + "%'");
                Interchange interchange = new Interchange();
                interchange = (Interchange)interchange.full(cursor);
                Intent intent = new Intent(this, CustomerEditorActivity.class);
                if (interchange != null){
                    Bundle bundle = new Bundle();
                    bundle.putString("interchange", interchange.toJson());
                    intent.putExtras(bundle);
                }
                startActivityForResult(intent, 0);
                break;
            case R.id.btn_cold_drink:

                break;
        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK){
//            exit();
//        }
//        return super.onKeyDown(keyCode, event);
//    }

    private void refreshCustomerInfo(){
        List<Field> fields = CreateSqlTools.getAnnoFieldList(SystemStatus.currentCustomer);
        customerEditorElements.clear();
        for (Field field : fields){
            CustomerEditorElement customerEditorElement = new CustomerEditorElement(SystemStatus.currentCustomer, field, this);
            customerEditorElements.add(customerEditorElement);
        }
        Collections.sort(customerEditorElements, new Comparator<CustomerEditorElement>() {
            @Override
            public int compare(CustomerEditorElement customerEditorElement, CustomerEditorElement customerEditorElement2) {
                Integer a = customerEditorElement.index();
                Integer b = customerEditorElement2.index();
                return a.compareTo(b);
            }
        });
        adapter = new ElementListViewAdapter(customerEditorElements);
        adapter.setContext(this);
        lvRouteList.setAdapter(adapter);
    }
}
